package com.sdut.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdut.material.mapper.UserRoleMapper;
import com.sdut.material.pojo.User;
import com.sdut.material.mapper.UserMapper;
import com.sdut.material.pojo.UserRole;
import com.sdut.material.pojo.vo.LayUISelectMVO;
import com.sdut.material.service.IRoleService;
import com.sdut.material.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdut.material.util.JSONResult;
import com.sdut.material.util.LayUITableResult;
import com.sdut.material.util.MD5Util;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yu
 * @since 2022-04-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private IRoleService roleService;
    @Override
    public List<String> selectRoleByName(String username) {
        return userMapper.selectRoleByName(username);
    }

    @Override
    public List<String> selectPermissionByName(String username) {
        return userMapper.selectPermissionByName(username);
    }

    @Override
    public LayUITableResult selectByPage(Long page, Long limit) {
        IPage<User> iPage = new Page<>(page,limit);
        IPage<User> result = userMapper.selectPage(iPage,new QueryWrapper<>());
        return LayUITableResult.ok(result.getTotal(),result.getRecords());
    }

    @Override
    public void insert(User user, String roles) {
        //首先将user插入到数据表当中去
        String salt = "sdut";
        String password = MD5Util.MD5Encode(user.getPassword() + salt);
        user.setPassword(password);
        userMapper.insert(user);
        //分割role数据并进行插入
        String[] roleArray = roles.split(",");
        for (String roleId : roleArray) {
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getId());
            userRole.setRoleId(Integer.parseInt(roleId));
            userRoleMapper.insert(userRole);
        }
    }

    @Override
    public Map<String, Object> selectUserAndRoles(Integer userId) {
        Map<String,Object> map = new HashMap<>();
        //根据id查询用户
        User user = userMapper.selectById(userId);
        map.put("user",user);
        //查询所有的角色
        List<LayUISelectMVO> layUISelectMVOList = roleService.selectAllRoles();
        map.put("allRoles",layUISelectMVOList);
        //查询该用户拥有的角色
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("role_id");
        queryWrapper.eq("user_id",userId);
        List list = userRoleMapper.selectObjs(queryWrapper);
        map.put("selectIds",list);
        return map;
    }

    @Override
    public void update(User user, String roles) {
        //根据用户更新用户
        String salt = "sdut";
        String password = MD5Util.MD5Encode(user.getPassword() + salt);
        user.setPassword(password);
        userMapper.updateById(user);
        //先删除用户所拥有的角色
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",user.getId());
        userRoleMapper.delete(queryWrapper);
        //为用户添加新的角色
        String[] rolearray = roles.split(",");
        for (String roleId : rolearray) {
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getId());
            userRole.setRoleId(Integer.parseInt(roleId));
            userRoleMapper.insert(userRole);
        }
    }

    @Override
    public void deleteById(Integer userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        userRoleMapper.delete(queryWrapper);
        userMapper.deleteById(userId);
    }

    @Override
    public JSONResult selectUserInfo(String username) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        return JSONResult.ok(user);
    }

    @Override
    public JSONResult updatePassword(String passwordFirst, String passwordSecond) {
        if (passwordFirst.equals(passwordSecond) == true) {
            Subject subject = SecurityUtils.getSubject();
            String username = (String)subject.getPrincipal();
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("username",username);
            User user = userMapper.selectOne(queryWrapper);
            String salt = "sdut";
            String password = MD5Util.MD5Encode(passwordFirst + salt);
            user.setPassword(password);
            userMapper.updateById(user);
            return JSONResult.ok("修改成功");
        }
        return JSONResult.ok("两次密码不一致");
    }
}
